package com.ruoyi.project.stop.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.redis.RedisCache;
import com.ruoyi.common.redis.RedisConst;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.stop.mapper.WarnConfigMapper;
import com.ruoyi.project.stop.domain.WarnConfig;
import com.ruoyi.project.stop.service.IWarnConfigService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 报警阈值设置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-26
 */
@Service
public class WarnConfigServiceImpl implements IWarnConfigService
{
    @Autowired
    private WarnConfigMapper warnConfigMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询报警阈值设置
     *
     * @param id 报警阈值设置主键
     * @return 报警阈值设置
     */
    @Override
    public WarnConfig selectWarnConfigById(Long id)
    {
        WarnConfig warnConfig = new WarnConfig();
        String redisStr =  JSON.toJSONString(redisCache.getCacheObject(RedisConst.WARN_CONFIG));
        if(ObjectUtil.isNotEmpty(redisStr) &&  !"null".equals(redisStr)){
            warnConfig = JSON.parseObject(redisStr, WarnConfig.class);
        }else {
            warnConfig = warnConfigMapper.selectWarnConfigById(id);
            redisCache.setCacheObject(RedisConst.WARN_CONFIG, warnConfig, 1, TimeUnit.HOURS);
        }
        return warnConfig;
    }

    /**
     * 查询报警阈值设置列表
     *
     * @param warnConfig 报警阈值设置
     * @return 报警阈值设置
     */
    @Override
    public List<WarnConfig> selectWarnConfigList(WarnConfig warnConfig)
    {
        return warnConfigMapper.selectWarnConfigList(warnConfig);
    }

    /**
     * 新增报警阈值设置
     *
     * @param warnConfig 报警阈值设置
     * @return 结果
     */
    @Override
    public int insertWarnConfig(WarnConfig warnConfig)
    {
        warnConfig.setCreateTime(DateUtils.getNowDate());
        return warnConfigMapper.insertWarnConfig(warnConfig);
    }

    /**
     * 修改报警阈值设置
     *
     * @param warnConfig 报警阈值设置
     * @return 结果
     */
    @Override
    public int updateWarnConfig(WarnConfig warnConfig)
    {
        warnConfig.setUpdateTime(DateUtils.getNowDate());
        int count = warnConfigMapper.updateWarnConfig(warnConfig);
        if(count != 0){
            // 保存redis
            redisCache.setCacheObject(RedisConst.WARN_CONFIG, warnConfig, 1, TimeUnit.HOURS);
        }
        return count;
    }

    /**
     * 批量删除报警阈值设置
     *
     * @param ids 需要删除的报警阈值设置主键
     * @return 结果
     */
    @Override
    public int deleteWarnConfigByIds(String ids)
    {
        return warnConfigMapper.deleteWarnConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除报警阈值设置信息
     *
     * @param id 报警阈值设置主键
     * @return 结果
     */
    @Override
    public int deleteWarnConfigById(Long id)
    {
        return warnConfigMapper.deleteWarnConfigById(id);
    }
}
